package definite.Guide.Book.JTabbedPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import definite.Guide.Book.ToggleButton.DiamondIcon;

public class TabSample {

	private static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW,
		Color.GREEN, Color.BLUE, Color.MAGENTA };

	private static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
		int count = tabbedPane.getTabCount();
		JButton btn = new JButton(label);
		btn.setBackground(colors[count]);
		tabbedPane.addTab(label, new DiamondIcon(colors[count], false, 20, 20),
				null, label);
		tabbedPane.setMnemonicAt(count, mnemonic);
	}

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("JTabbedPane Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				// create tabbedPane
				JTabbedPane tabbedPane = new JTabbedPane();
				tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
				String titles[] =
					{ "General", "Security", "Content", "Connection",
						"Programs", "Advanced" };
				int mnemonic[] =
					{ KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C,
						KeyEvent.VK_O, KeyEvent.VK_P, KeyEvent.VK_A };
				for (int i = 0, n = titles.length; i < n; i++) {
					add(tabbedPane, titles[i], mnemonic[i]);
				}

				ChangeListener change = new ChangeListener() {

					@Override
					public void stateChanged(ChangeEvent e) {
						JTabbedPane tbPane = (JTabbedPane) e.getSource();
						int index = tbPane.getSelectedIndex();
						System.out.println("Selected changed : " + index);
						tbPane.getTitleAt(index);
					}
				};
				tabbedPane.addChangeListener(change);

				frame.add(tabbedPane, BorderLayout.NORTH);
				frame.setSize(400, 200);
				frame.setLocationRelativeTo(null);

				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}

}
